jQuery.noConflict();
var local_id_sa;
var ArrayActivity = new Array();
var is_show_payment = true;
var flag = null;
var id_interval = null;
var is_show_table_activity = false;
var list_id_activity_exist = new Array();

function setVisiable_table_activity(b){
    if(b){
        jQuery("#latestnews").slideDown("fast", function() {
            is_show_table_activity =true;
        });
    }else{
        jQuery("#latestnews").slideUp("fast", function() {
            is_show_table_activity = false;
        });
    }
}
function setVisiable_payment_form(b){
    if(b){
        jQuery("#cc-container").slideDown("fast", function() {
            jQuery("#cc-contents").slideDown("fast");
            jQuery("#cc-header").slideDown("fast");
            is_show_payment =true;
        });
    }else{
        jQuery("#cc-container").slideUp("fast", function() {
            jQuery("#cc-header").slideUp("fast");
            jQuery("#cc-contents").slideUp("fast");
            is_show_payment = false;
        });
    }
}
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
function getDataAjax(){
    if(flag == null){
        ArrayActivity.splice(0,ArrayActivity.length);
    if(local_id_sa == null){
        local_id_sa = getUrlVars()["scheduleid"];
    }
    var url='http://localhost:8080/NLPP_SEM4/LoadDataServlet?type=getactivitiesbyid&scheduleid='+local_id_sa;//danh duong dan den servlet mà khi tao truy?n lên 1 cái id SA nó tr? v? m?t XML ch?a các Activity c?a SA ð?y
    aeRequest = createXmlHttpRequestObject();
    aeRequest.open("POST", url, true);
    aeRequest.setRequestHeader("Content-type",
        "application/x-www-form-urlencoded");
    aeRequest.setRequestHeader("Content-length", 0);
    aeRequest.setRequestHeader("Connection", "close");
    aeRequest.onreadystatechange = function() {
        if (aeRequest.readyState == 4) {
            if (aeRequest.status == 200) {
                var response = createResponseXML(aeRequest.responseText);
                var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;
                if(result == 'error'){
                    alert("something wrong");
                }else{
                    var Activitys = response.getElementsByTagName("activity");
                    for(var i = 0; i < Activitys.length;i++){
                        var id= Activitys[i].getElementsByTagName("activityid")[0].firstChild.nodeValue;
                        var currentPeople = Activitys[i].getElementsByTagName("activitycurrentpeople")[0].firstChild.nodeValue;
                        var maxPeople = Activitys[i].getElementsByTagName("activitylimittedpeople")[0].firstChild.nodeValue;
                        ArrayActivity.push(new Array(id,currentPeople,maxPeople));
                    }
                    drawData(ArrayActivity);
                }

            }
        }

    };
    aeRequest.send(null);
    }
    
}
function drawData(arrayInfor){
    for(var i = 0 ; i < arrayInfor.length;i++){
        
        var slot = parseInt(arrayInfor[i][2]) - parseInt(arrayInfor[i][1]);
        if(slot == 0){
            jQuery("#"+arrayInfor[i][0]).html("<label color='red'>Full Slot</label>");
        }else{
            var html = "<label color='blue'>"+ slot +" slot available</label><br/>";
            //dong o duoi ko thay hien
            html += "<input type=\"button\"  class=\"button_join\" value=\"Join\"  id=\"aJoinactivity\" onclick=\"openPayment('"+arrayInfor[i][0] +"')\"></input>";
            //alert(html);
            jQuery("#"+arrayInfor[i][0]).html(html);
        }
    }
}

function autoLoadData(){
    
        id_interval = setInterval("getDataAjax()", 1000);

    
      
}

function closePayment(){
    setVisiable_payment_form(false);
    setVisiable_table_activity(true);
    flag = null;
}
function openPayment(id){
    setVisiable_table_activity(false);
    jQuery("#id_hidden").val(id);
    setVisiable_payment_form(true);
    flag = 'going';
}


function doPayment(){
    if(validatePayMent()){

        var id = jQuery("#id_hidden").val();
        //alert(id);
        var typeOfCard = jQuery("#cc-types").val();
        var url='http://localhost:8080/NLPP_SEM4/JoinToActivityServlet?type=joinactivity&action=add&activityid='+id+'&paymentid='+typeOfCard;
        //alert(url);
        //danh duong dan vao cai luc ma user no an vao nut pay ment co gancai id day
        aeRequest = createXmlHttpRequestObject();
        aeRequest.open("POST", url, true);
        aeRequest.setRequestHeader("Content-type",
            "application/x-www-form-urlencoded");
        aeRequest.setRequestHeader("Content-length", 0);
        aeRequest.setRequestHeader("Connection", "close");
        aeRequest.onreadystatechange = function() {
            if (aeRequest.readyState == 4) {
                if (aeRequest.status == 200) {
                    alert(aeRequest.responseText);//
                    var response = createResponseXML(aeRequest.responseText);
                    var result = response.getElementsByTagName("result")[0].firstChild.nodeValue;
                   
                    if(result == 'null'){
                        jQuery("header_payment").html("<label color=\"red;\">please choose card</label>");
                    }else{
                        jQuery("header_payment").html("<label color=\"red;\">Payment success</label>");
                        setTimeout("checkout()", 2000);
                        list_id_activity_exist.push(id)
                        flag = null;
                    }
                }

            }
        };
        aeRequest.send(null);
   

    }
    
    
}
function checkout(){
    setVisiable_payment_form(false);
    setVisiable_table_activity(true);
}


function validatePayMent(){
    
    var selectType = jQuery("#cc-types").val();
   
    if(selectType == null || selectType.length == 0){
        jQuery("#header_payment").html("<label color=\"red;\">please choose card</label>");
        return false;
    }
    var numberCard = jQuery("#card-number").val();
    
    var ccReg = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/;
    if(!ccReg.test(numberCard)){
        jQuery("#header_payment").html("<label color=\"red;\">Number Card is invalid</label>");
        
        return false;
    }
//    var cvv = jQuery("#cvv").val();
//    var numericReg = /^[0-9]{3}$/;
//    if(!numericReg.test(cvv)) {
//        jQuery("#header_payment").html("<label color=\"red;\">Invalid CVV</label>");
//        return false;
//    }

//    var month = jQuery("#cc-month").val();
//    var year = jQuery("#cc_year").val();
//    if(month == null || month.length == 0){
//        jQuery("#header_payment").html("<label color=\"red;\">Month is invalid</label>");
//        return false;
//    }
//    if(year == null || year.length == 0){
//        jQuery("#header_payment").html("<label color=\"red;\">Year is invalid</label>");
//        return false;
//    }
    return true;
}